package gui;



import Entities.ExtendEntity;
import common.ChatIF;
import common.CommonMethods;


import java.awt.Font;
import java.awt.Frame;
import java.awt.Toolkit;
import java.awt.event.WindowEvent;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;


import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JButton;

import com.toedter.calendar.JDateChooser;




import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.BorderLayout;
/**
 * 
 * @author Idan Mann
 * This is a GUI class that is reused across the project
 * It presents the user the current deadline and a visual calendar 
 * from which it may choose a new date.
 *
 */
public class ExtendDeadline extends JFrame implements ChatIF
{
	private JButton btnCancel;
	private JButton btnSend;
	private JTextArea textAreaReason;
	private String oldDeadline;
	private String table;
	private JFrame myself;
	private JButton extendButton;
	private JTextField oldDeadlinetextField;
	public JDateChooser newdeadline;
	private String rid;
	private String currentStatus;
	private String wasExtendedColumn;
	private String requestIDColumn;
	
	
	public ExtendDeadline(String deadline, JButton extendButton, String rid, String currentStatus, String table, String wasExtendedColumn, String requestIDColumn)
	{
		this.oldDeadline = deadline;
		this.table = table;
		this.wasExtendedColumn = wasExtendedColumn;
		this.requestIDColumn = requestIDColumn;
		this.myself = this;
		this.extendButton = extendButton;
		this.rid = rid;
		this.currentStatus = currentStatus;
		

		initialize();
	}
	
		private void initialize()
		{
			final Date today = new Date();
			final DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
			
			
			getContentPane().setLayout(null);
			
			newdeadline = new JDateChooser();
			newdeadline.setDate(today);
			newdeadline.setBounds(443, 82, 174, 20);
			getContentPane().add(newdeadline);
			
			JLabel lblExtendDeadline = new JLabel("Extend Deadline");
			lblExtendDeadline.setBounds(267, 27, 142, 25);
			lblExtendDeadline.setFont(new Font("Tahoma", Font.PLAIN, 16));
			getContentPane().add(lblExtendDeadline);
			
			JLabel lblCurrentDeadline = new JLabel("Current Deadline");
			lblCurrentDeadline.setBounds(33, 85, 99, 14);
			getContentPane().add(lblCurrentDeadline);
			
			oldDeadlinetextField = new JTextField();
			oldDeadlinetextField.setBounds(146, 82, 123, 20);
			oldDeadlinetextField.setEditable(false);
			getContentPane().add(oldDeadlinetextField);
			oldDeadlinetextField.setColumns(10);
			
			JScrollPane scrollPane = new JScrollPane();
			scrollPane.setBounds(49, 147, 612, 94);
			getContentPane().add(scrollPane);
			
			textAreaReason = new JTextArea();
			scrollPane.setViewportView(textAreaReason);
			
			JLabel lblReasonForPushing = new JLabel("Reason for pushing the deadline");
			lblReasonForPushing.setBounds(50, 124, 219, 14);
			getContentPane().add(lblReasonForPushing);
			
			btnSend = new JButton("Send");
			btnSend.addActionListener(new ActionListener() 
			{
				public void actionPerformed(ActionEvent e)
				{
					try
					{
						Date currentDeadline = dateFormat.parse(oldDeadline);
						currentDeadline.setHours(23);
						currentDeadline.setMinutes(59);
						currentDeadline.setSeconds(59);
						
						if (newdeadline.getDate().equals(currentDeadline) || newdeadline.getDate().before(currentDeadline))
						{
							(new Dialog ("New deadline cannot be in the past.")).setVisible(true);
						}
						else
							if (textAreaReason.getText().isEmpty())
							{
								(new Dialog ("Please enter a reason first.")).setVisible(true);
							}
							else // Valid.
							{
								
								ExtendEntity deadlineRequest = new ExtendEntity();
								deadlineRequest.setNewDeadline(dateFormat.format(newdeadline.getDate()));
								deadlineRequest.setOldStatus(currentStatus);
								deadlineRequest.setReason(textAreaReason.getText());
								deadlineRequest.setRequestID(rid);
								deadlineRequest.setCurrentTable(table);
								deadlineRequest.setWasExtendedColumnName(wasExtendedColumn);
								deadlineRequest.setRequestIDColumnName(requestIDColumn);
								
								ArrayList<Object> requestNewDeadline = new ArrayList<Object>();
								requestNewDeadline.add("requestNewDeadline");
								
								requestNewDeadline.add(deadlineRequest);
								AcademicFrame.myclient.sendMessageToServer(requestNewDeadline);
								myself.dispose();
								(new Dialog ("Your request was sent.")).setVisible(true);
								
								
							}
						
					}
					catch (ParseException e1) {
						// TODO Auto-generated catch block
						//e1.printStackTrace();
					}
						
					
				}
			});
			btnSend.setBounds(372, 290, 91, 23);
			getContentPane().add(btnSend);
			
			btnCancel = new JButton("Cancel");
			btnCancel.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) 
				{
					ArrayList<Object> requestNewDeadline = new ArrayList<Object>();
					requestNewDeadline.add("canceledNewDeadline");
					AcademicFrame.myclient.sendMessageToServer(requestNewDeadline);
					
					myself.setVisible(false);
					myself.dispose();
				}
			});
			btnCancel.setBounds(186, 290, 91, 23);
			getContentPane().add(btnCancel);
			
			oldDeadlinetextField.setText(oldDeadline);
			
			JLabel lblNewLabel = new JLabel("New Deadline");
			lblNewLabel.setBounds(353, 85, 80, 14);
			getContentPane().add(lblNewLabel);
			
			extendButton.setEnabled(false);
		}

	public String getrequestID()
	{
		return rid;
	}
	
		@Override
	public void display(Object message)
	{
			ArrayList<Object> answer = new ArrayList<Object>();
			answer = (ArrayList<Object>)message;
			
			
			if (	((String)(answer.get(0))).equalsIgnoreCase("extentionRequestWasSent")	)
			{
				
			}
	}
}
